#include "settings.h"
#include "connection.h"
#include "ui_settings.h"
#include "string"
#include "QFile"
#include "QDir"

Settings::Settings(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Settings)
{
    ui->setupUi(this);
    setting = new QSettings(QDir::currentPath() + "/Settings/connection.ini", QSettings::IniFormat);

    ui->driverSelector->setCurrentText(setting->value("Driver", "").toString());
    ui->hostName->setText(setting->value("HostName", "").toString());
    ui->dbName->setText(setting->value("DBName", "").toString());
    ui->userName->setText(setting->value("UserName", "").toString());
    ui->password->setText(setting->value("Password", "").toString());
    ui->portNumber->setText(setting->value("Port", "").toString());
}

Settings::~Settings()
{
    delete ui;
}

void Settings::on_pushButton_clicked()
{

    createConnection(ui->driverSelector->currentText(), ui->hostName->text(), ui->dbName->text(), ui->userName->text(), ui->password->text(), ui->portNumber->text().toInt());
    setting->setValue("Driver", ui->driverSelector->currentText());
    setting->setValue("HostName", ui->hostName->text());
    setting->setValue("DBName", ui->dbName->text());
    setting->setValue("UserName", ui->userName->text());
    setting->setValue("Password", ui->password->text());
    setting->setValue("Port", ui->portNumber->text());
    setting->sync();
    if(connected)
    {
        authorization = new Authorization;
        authorization -> show();
        this->close();
    }


    /*createConnection(ui->driverSelector->currentText(), ui->hostName->text(), ui->dbName->text(), ui->userName->text(), ui->password->text(), ui->portNumber->text().toInt());
    QString line;
    QFile in("Settings/connection.conf");
    QStringList list;
    if(in.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        while(!in.atEnd())
        {
            QString line = in.readLine();
            list = line.split(" ");
            qDebug() << list;
        }
        createConnection(list[0], list[1], list[2], list[3], list[4], list[5].toInt());
        if(connected)
        {
            authorization = new Authorization;
            authorization -> show();
        }
    }
    else
    {

        std::ofstream out("Settings/connection.conf");
        if(out.is_open())
        {
            out << ui->driverSelector->currentText().toStdString() << std::endl;
            out << ui->hostName->text().toStdString() << std::endl;
            out << ui->dbName->text().toStdString() << std::endl;
            out << ui->userName->text().toStdString() << std::endl;
            out << ui->password->text().toStdString() << std::endl;
            out << ui->portNumber->text().toStdString() << std::endl;
        }
        else
        {
            qDebug() << "не удалость открыть файл";
        }
        out.close();

    }*/
}


void Settings::on_checkBox_clicked(bool checked)
{
    if(checked)
        ui->password->setEchoMode(QLineEdit::Normal);
    else
        ui->password->setEchoMode(QLineEdit::Password);
}

